package com.kangyi.app.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kangyi.app.mapper.ActivitySignupMapper;
import com.kangyi.app.mapper.CharityMapper;
import com.kangyi.app.mapper.PublicActivityImgMapper;
import com.kangyi.app.service.CharityService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.ActivitySignupDTO;
import com.kangyi.dto.CharityListDTO;
import com.kangyi.entity.ActivitySignup;
import com.kangyi.entity.PublicActivityImg;
import com.kangyi.exception.AccountException;
import com.kangyi.exception.ActivitySignupException;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.result.PageResult;
import com.kangyi.utils.AuthUtil;
import com.kangyi.vo.CharityDetailVO;
import com.kangyi.vo.CharityListVO;
import com.kangyi.vo.PublicActivityDetailVO;
import com.kangyi.vo.PublicActivityImgVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class CharityServiceImpl implements CharityService {

    @Autowired
    private CharityMapper charityMapper;

    @Autowired
    private ActivitySignupMapper activitySignupMapper;

    @Autowired
    private PublicActivityImgMapper publicActivityImgMapper;

    @Override
    public PageResult list(CharityListDTO charityListDTO) {
        PageHelper.startPage(charityListDTO.getPage(),charityListDTO.getPageSize());
        Page<CharityListVO> page = charityMapper.list(charityListDTO);
        long total = page.getTotal();
        List<CharityListVO> result = page.getResult();
        for (CharityListVO vo : result) {
            Integer participants = activitySignupMapper.getTotal(vo.getId());
            vo.setParticipants(participants);
            String image = publicActivityImgMapper.getImage(vo.getId());
            vo.setImage(image);
        }
        return new PageResult(total,result);
    }

    @Override
    public CharityDetailVO detailPublicActivity(Integer id) {
        CharityDetailVO charityDetailVO = charityMapper.detailPublicActivity(id);
        if (charityDetailVO == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        List<PublicActivityImgVO> publicActivityImgVOList = publicActivityImgMapper.selectByPublicActivityId(id);

        charityDetailVO.setPublicActivityImgVOList(publicActivityImgVOList);

        return charityDetailVO;
    }

    @Override
    public void register(ActivitySignupDTO activitySignupDTO) {
        //查询是否有过报名
        ActivitySignup activitySignup = activitySignupMapper.getByGuestsStayingIdAndActivityId(activitySignupDTO.getGuestsStayingId(),activitySignupDTO.getActivityId());
        if (activitySignup != null) {
            throw new ActivitySignupException(MessageConstant.YOU_HAVE_ALREADY_REGISTERED_FOR_THIS_EVENT);
        }
        //设置报名状态，时间
        ActivitySignup build = ActivitySignup.builder()
                .activityId(activitySignupDTO.getActivityId())
                .guestsStayingId(activitySignupDTO.getGuestsStayingId())
                .userId(activitySignupDTO.getUserId())
                .signupTime(LocalDateTime.now())
                .status(ActivitySignup.PENDING_REVIEW)
                .build();
        //报名
        activitySignupMapper.signUpActivity(build);
    }

    @Override
    public void cancelRegistration(Integer registrationId) {
        Integer currentId = BaseContext.getCurrentId();
        AuthUtil.validateUserPermission(currentId);

        ActivitySignup activitySignup = activitySignupMapper.getById(registrationId);
        if (activitySignup == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        Integer userId = activitySignup.getUserId();
        if (userId != currentId) {
            throw new AccountException(MessageConstant.CANNOT_MANIPULATE_OTHER_PEOPLE_ACCOUNTS);
        }

        activitySignupMapper.deleteActivitySignup(registrationId);
    }
}
